package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.vo.CurCourseBaseResp;
import com.macro.mall.common.vo.ParentVO;
import com.macro.mall.mapper.CurCourseBaseMapper;
import com.macro.mall.mapper.CurCourseStatMapper;
import com.macro.mall.model.CurCourseBase;
import com.macro.mall.model.CurCourseStat;
import com.macro.mall.service.CurCourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CurCourseBase的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class CurCourseBaseServiceImpl implements CurCourseBaseService {

    @Value("${log.isDebugEnabled:false}")
    private boolean isDebugEnabled;

    @Autowired
    private CurCourseBaseMapper curCourseBaseMapper;
    @Autowired
    private CurCourseStatMapper curCourseStatMapper;

    @Override
    public List<CurCourseBase> find(CurCourseBase value) {
        //TODO这里可以做通过Assist做添加查询
        List<CurCourseBase> result = curCourseBaseMapper.selectCurCourseBase(null);
        if (isDebugEnabled) {
            log.debug("执行获取CurCourseBase数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public List<CurCourseBaseResp> findBycondition(String courseName, Integer courseType, String deptName, Integer pageNum, Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("ccb.create_time desc");
        List<CurCourseBaseResp> result = null;
        try {
            result = curCourseBaseMapper.findBycondition(courseName, courseType, deptName);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);

        }
        if (isDebugEnabled) {
            log.debug("执行获取CurCourseBase数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public CurCourseBaseResp findOne(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CurCourseBase的id获得CurCourseBase对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过CurCourseBase的id获得CurCourseBase对象-->失败:id不能为空");
        }
        CurCourseBaseResp result = null;
        try {
            result = curCourseBaseMapper.selectAdminCurCourseBaseById(id);
            if (result != null) {
                List<ParentVO> parent = curCourseBaseMapper.selectParent(result.getCourseType());
                result.setTypeList(parent);
            }
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CurCourseBase的id获得CurCourseBase对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(CurCourseBase value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将CurCourseBase中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            result = curCourseBaseMapper.insertNotNullCurCourseBase(value);
            CurCourseStat curCourseStat = new CurCourseStat();
            curCourseStat.setCourseId(value.getId());
            curCourseStatMapper.insertNotNullCurCourseStat(curCourseStat);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将CurCourseBase中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(CurCourseBase value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CurCourseBase的id更新CurCourseBase中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过CurCourseBase的id更新CurCourseBase中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            result = curCourseBaseMapper.updateNotNullCurCourseBaseById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CurCourseBase的id更新CurCourseBase中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CurCourseBase的id删除CurCourseBase-->失败:id不能为空");
            }
            Asserts.fail("执行通过CurCourseBase的id删除CurCourseBase-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = curCourseBaseMapper.deleteCurCourseBaseById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CurCourseBase的id删除CurCourseBase-->结果:", result);
        }
        return CommonResult.success(null);
    }


}